<?php

namespace app\api\service;

class JsonService
{
    private static $SUCCESSFUL_DEFAULT_MESSAGE = 'SUCCESS';

    private static $FAIL_DEFAULT_MESSAGE = 'FAIL';


    public static function successlayui($count = 0, $data = [], $msg = '')
    {
        if (is_array($count)) {
            if (isset($count['data'])) $data = $count['data'];
            if (isset($count['count'])) $count = $count['count'];
        }
        if (false == is_string($msg)) {
            $data = $msg;
            $msg = self::$SUCCESSFUL_DEFAULT_MESSAGE;
        }
        return self::result(0, $msg, $data, $count);
    }

    /**
     * 结果
     * @param $code
     * @param string $message
     * @param array $data
     * @param bool $success
     * @return \think\response\Json
     */
    public static function result($code, $message = '', $data = null, $success = true)
    {
        return json(compact('code', 'message', 'data', 'success'));
    }

    public static function successful($message = 'ok', $data = null, $status = 200)
    {
        if (false == is_string($message)) {
            $data = $message;
            $message = self::$SUCCESSFUL_DEFAULT_MESSAGE;
        }
        return self::result($status, $message, $data);
    }

    public static function status($status, $message, $result = null)
    {
        $status = strtoupper($status);
        if (true == is_array($message)) {
            $result = $message;
            $message = self::$SUCCESSFUL_DEFAULT_MESSAGE;
        }
        return self::result(200, $message, compact('status', 'result'));
    }

    /***
     * 失败
     * @param $message
     * @param array $data
     * @param int $code
     * @param bool $success
     * @return \think\response\Json
     */
    public static function fail($message, $data = null, $code = 400, $success = false)
    {
        if (true == is_array($message)) {
            $data = $message;
            $message = self::$FAIL_DEFAULT_MESSAGE;
        }
        return self::result($code, $message, $data, $success);
    }

    /**
     * 成功
     * @param $message
     * @param array $data
     * @return \think\response\Json
     */
    public static function success($message, $data = null)
    {
        if (true == is_array($message)) {
            $data = $message;
            $message = self::$SUCCESSFUL_DEFAULT_MESSAGE;
        }
        return self::result(200, $message, $data, true);
    }


}